package org.ly.dq.module;

import org.ly.dq.bean.PingPai;
import org.ly.sys.bean.User;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.service.EntityService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@At("/PingPai")
@IocBean(fields={"dao"})
public class PingPaiModule extends EntityService<PingPai>{

    private static final Log log = Logs.get();
	
	@At
	@Ok("jsp:views.dq.listPingPai")
	
	public Object list(@Param("page") int page ,@Param("rows") int rows){
		if (rows < 1)
			rows = 10;
		if(page < 1) 
		    page = 1;
		Pager pager = dao().createPager(page, rows);
		List<PingPai> list = dao().query(PingPai.class, null, pager);
		Map<String, Object> map = new HashMap<String, Object>();
		if (pager != null) {
			pager.setRecordCount(dao().count(PingPai.class));
			map.put("pager", pager);
		}
		map.put("list", list);
		return map;
	}
	
	@At
	@Ok("jsp:views.dq.addPingPai")
	public Object add() {
        Map<String,Object> map = new HashMap<String, Object>();
        List<User> users = dao().query(User.class,null);
        map.put("users",users);
        return  map;
    }

    @At
    @Ok("jsp:views.dq.editPingPai")
    public Object edit(@Param("pingPaiId") int pingPaiId) {
        Map<String,Object> map = new HashMap<String, Object>();
        List<User> users = dao().query(User.class,null);
        PingPai pingPai = dao().fetch(PingPai.class,pingPaiId);
        map.put("users",users);
        map.put("pingPai", pingPai);
        return  map;
    }
	
	@At
	@Ok("redirect:/PingPai/list")
	public boolean save(@Param("..") PingPai obj){
		try{
			dao().insert(obj);
			return true;
		}catch (Throwable e) {
			if (log.isDebugEnabled())
				log.debug("E!!",e);
			return false;
		}
	}
	
	@At
	@Ok("redirect:/PingPai/list")
	public void delete(@Param("..") PingPai obj){
		try{
			dao().delete(obj);
		}catch (Throwable e) {
			if (log.isDebugEnabled())
				log.debug("E!!",e);
		}
	}
	
	@At
    @Ok("redirect:/PingPai/list")
	public boolean update(@Param("..") PingPai obj){
		try{
			dao().update(obj);
			return true;
		}catch (Throwable e) {
			if (log.isDebugEnabled())
				log.debug("E!!",e);
			return false;
		}
	}
}